
package com.x52im.rbchat.logic.qrcode.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.x52im.rbchat.R;

import androidx.annotation.FloatRange;

/**
 * 全面屏状态栏设置。
 *
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @see  com.eva.android.x.XToolKits
 */
public final class StatusBarUtils {

    private StatusBarUtils(){
        throw new AssertionError();
    }

    public static void immersiveStatusBar(Activity activity, View toolbar) {
        immersiveStatusBar(activity, toolbar,0.0f);
    }

    /**
     * 设置全面屏情况下的状态栏（下移用户标题栏，并让整个界面全屏从而在用户标题栏下方透过实现沉浸式效果）。
     * <p>
     * 不同于{@link com.eva.android.x.XToolKits#setStatusBarTranslucent(Activity, boolean)}方法，本方法
     * 既可以实现沉浸式效果，又可以将自定义标题栏自动避开系统状态栏并下推显示（防止跟系统状态栏重叠）。
     * <p>
     * 注意：受制于国内各厂商全面屏的api兼容性问题，目前本方法和XToolKits中的相关方法需要长期演示和测试，终有一日两者
     * 将进行合并，暂时各自用于相应的场景进行更多新技术测试和演进。
     *
     * @param activity
     * @param toolbar
     * @param alpha
     */
    public static void immersiveStatusBar(Activity activity, View toolbar, @FloatRange(from = 0.0, to = 1.0) float alpha) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }

        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        ViewGroup decorView = (ViewGroup) window.getDecorView();
        ViewGroup contentView = window.getDecorView().findViewById(Window.ID_ANDROID_CONTENT);
        View rootView = contentView.getChildAt(0);
        if (rootView != null) {
            rootView.setFitsSystemWindows(false);
        }
        if(toolbar!=null){
            toolbar.setPadding(0, getStatusBarHeight(activity),0,0);
        }

        decorView.addView(createStatusBarView(activity,alpha));
    }

    private static View createStatusBarView(Activity activity, @FloatRange(from = 0.0, to = 1.0) float alpha) {
        // 绘制一个和状态栏一样高的矩形
        View statusBarView = new View(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(Color.argb((int) (alpha * 255), 0, 0, 0));
        statusBarView.setId(R.id.translucent_view);
        return statusBarView;
    }

    /** 获取状态栏高度 */
    public static int getStatusBarHeight(Context context) {
        int statusBarHeight = getStatusBarHeightSystem(context);
        Log.i("statusBarHeight", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@getStatusBarHeight="+statusBarHeight);
        if(statusBarHeight == -1)
            statusBarHeight = context.getResources().getDimensionPixelSize(R.dimen.status_bar_height);
        return statusBarHeight;
    }

    /**
     * 通过标准的方法获取系统状态栏高度，支持刘海屏。
     *
     * @param context 上下文
     * @return 如果正常获取就返回对应的值，否则返回-1
     */
    public static int getStatusBarHeightSystem(Context context) {
        try {
            Resources resources = context.getResources();
            int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        } catch (Exception e){
            Log.w("getStatusBarHeightEx", e);
            return -1;
        }
    }

    /**
     * 通过标准的方法获取系统状态栏高度，支持小米的全面屏（底部的软按键没有的情况）。
     *
     * @param c 上下文
     * @return 如果正常获取就返回对应的值，否则返回-1
     */
    public static int getNavigationBarHeight(Context c) {
        try {
            Resources resources = c.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        } catch (Exception e){
            Log.w("getNavigationBarHeight", e);
            return 0;
        }
    }
}
